package cn.lyjuan.android.demofive.ui.slide.scroll;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.ListView;
import cn.lyjuan.android.demofive.R;

/**
 * Created by chad on 2016/4/17.
 */
public class DrawerActivity extends Activity implements ChildFragment.OpenDrawerCallback
{
    DrawerLayout drawer;

    FrameLayout main;

    ListView menu;

    String[] titles = new String[]{"First", "Second", "Third", "Fourth"};

    ArrayAdapter adp;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        this.setContentView(R.layout.ui_scroll_drawer);

        init();
    }

    void init()
    {
        initComponent();
        initDate();
        initBind();
    }

    void initComponent()
    {
        drawer = (DrawerLayout) this.findViewById(R.id.ui_scroll_drawer);
        main = (FrameLayout) this.findViewById(R.id.ui_scroll_drawer_frame);
        menu = (ListView) this.findViewById(R.id.ui_scroll_drawer_menu);
    }

    void initDate()
    {
        adp = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, titles);
        menu.setAdapter(adp);

        selectMenuItem(0);
    }

    void initBind()
    {
        menu.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                Log.w(DrawerActivity.class.getSimpleName(), "menu item onclick position >> " + position);
                // 切换界面
                selectMenuItem(position);
            }
        });
    }

    void selectMenuItem(int position)
    {
        Fragment child = new ChildFragment();
        DrawerActivity.this.getFragmentManager().beginTransaction().replace(R.id.ui_scroll_drawer_frame, child).commit();

        DrawerActivity.this.getActionBar().setTitle(titles[position]);

        menu.setItemChecked(position, true);
        // 关闭Drawer
        drawer.closeDrawer(menu);
    }

    @Override
    public void openDrawer()
    {
        drawer.openDrawer(menu);
    }


}
